package com.eascs.spider.zhaopin.actor;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eascs.spider.zhaopin.util.MongodbUtil;
import com.eascs.spider.zhaopin.util.NetUtil;
import com.eascs.spider.zhaopin.util.PropertiesUtil;
import com.eascs.spider.zhaopin.util.RedisUtil;
import com.eascs.spider.zhaopin.util.StringUtil;

/**
 * 51job执行器，关键词1个+年龄+期望地区1个
 * @author Administrator
 *
 */
public class Job51Actor implements Runnable{

	private Logger logger = LoggerFactory.getLogger(Job51Actor.class);
	private CloseableHttpClient client = NetUtil.initHttpClient();
	private String cookie;
	private String keyword;//关键词
	private int ageMin;//最小年龄
	private int ageMax;//最大年龄
	private String expAddress;//期望工作地 ,若为省份则值为省份编码,若为省会城市则值为 南宁|140200
	private int duplicate_count=0;//重复计数,如果超过10个则中断线程
	
	public Job51Actor(String cookie , String keyword , String expAddress ,int ageMin , int ageMax){
		this.cookie = cookie;
		this.keyword = keyword;
		this.expAddress = expAddress;
		this.ageMin = ageMin;
		this.ageMax = ageMax;
	}
	
	@Override
	public void run(){
		String url = "http://ehire.51job.com/Candidate/SearchResumeIndexNew.aspx";
		String referer = "http://ehire.51job.com/Candidate/SearchResumeIndexNew.aspx";
		Map<String, String> header = new HashMap<>();
		header.put("Referer", referer);
		header.put("Cookie", cookie);
		header.put("Host", "ehire.51job.com");
		String html = NetUtil.loadPageRawTextByGet(url, client, header, null);
		Element vs = Jsoup.parse(html).getElementById("__VIEWSTATE");
		if(vs == null){
			logger.info("没有获取到viewstate,关键词:{},期望工作地区:{},html:{}" , keyword , expAddress ,html);
			return ;
		}
		String view_state = Jsoup.parse(html).getElementById("__VIEWSTATE").val();
    	
		String searchUrl = "http://ehire.51job.com/Candidate/SearchResumeNew.aspx";
		Map<String, String> searchHeader = new HashMap<>();
		searchHeader.put("Cookie", cookie);
		searchHeader.put("Referer", "http://ehire.51job.com/Candidate/SearchResumeIndexNew.aspx");
		searchHeader.put("Origin", "http://ehire.51job.com");
		Map<String, String> param = new HashMap<>();
		param.put("__VIEWSTATE", view_state);
		param.put("search_area_hid", "");
		param.put("sex_ch", "99|不限");
		param.put("sex_en", "99|Unlimited");
		param.put("hidSearchEngineid", "");
		param.put("send_cycle", "1");
		param.put("send_time", "7");
		param.put("send_sum", "10");
		param.put("hidWhere", "");
		param.put("searchValueHid", keyword + "##1#######"+ageMin+"#"+ageMax+"##99############1#0##"+expAddress+"#0#0#0");
		param.put("showGuide", "");
		//				              行长##0#######30#45##99############1#0##210000#0#0#0
		//searchValueHid :业务经理##1#######30#45##99############1#0##210000#0#0#0
		//						  经验                   30-45岁                                                          山西
		//					 行长##0##########99############1#0##210200#0#0#0
		String searchHtml = NetUtil.loadPageRawTextByPost(searchUrl, client, searchHeader, param, "utf-8");
		if(etl(searchHtml ,null))
			turnPage(searchHtml);
	}
	
	
	
	
	//从请求的第一个页面，获取参数，进行翻页请求
	public void turnPage(String firstPageHtml ){
    	try {
    		Document document = Jsoup.parse(firstPageHtml);
    		Element pageElement = document.getElementById("pagerBottomNew_btnNum_ma");
    		if(pageElement == null){
    			logger.info("过滤器入参：关键词：{},期望工作地点：{},年龄：{}-{} ,不需翻页" , keyword , expAddress , ageMin ,ageMax);
    			return ;
    		}
    		int pages = Integer.valueOf(document.getElementById("pagerBottomNew_btnNum_ma").text());
    		logger.info("过滤器入参：关键词：{},期望工作地点：{},年龄：{}-{},共需翻页次数：{}" , keyword , expAddress , ageMin ,ageMax ,pages);
    		Map<String, String> param = packageTurnpageParam(document);
    		if(pages<2)
    			return;
    		for(int i=2 ;i<=pages;i++){
    			if(duplicate_count >= 10){
    				logger.warn("关键词：{},期望工作地点：{},重复计数超过10次，中断线程" ,keyword ,expAddress);
    				return;
    			}
    			logger.info("关键词：{},期望工作地点：{},年龄：{}-{},第{}页" , keyword , expAddress , ageMin ,ageMax ,i);
    			int currentPage =0;
    			if(i>5 && i<pages){
    				currentPage =4;
    			}else if(i<=5){
    				currentPage = i;
    			}else if(i==pages){
    				currentPage = 5;
    			}
    			param.put("__EVENTTARGET", "pagerBottomNew$btnNum" + currentPage);
    			String url = "http://ehire.51job.com/Candidate/SearchResumeNew.aspx";
    			Map<String, String> header = new HashMap<>();
    			header.put("Referer", "http://ehire.51job.com/Candidate/SearchResumeNew.aspx");
    			header.put("Origin", "http://ehire.51job.com");
    			header.put("Host", "ehire.51job.com");
    			header.put("Cookie", cookie);
    			String html = NetUtil.loadPageRawTextByPost(url, client, header, param, "utf-8");
    			etl(html,param);
    			Thread.sleep(3000);
    		}
		} catch (Exception e) {
			logger.error("翻页发生异常,html:{}" ,firstPageHtml ,e);
		}
    }
    
    /**
     * 解析单个页面
     * @param html
     * @return 返回false表示无需翻页
     */
    public boolean etl(String html ,Map<String, String> param){
    	if(html.contains("Object moved")){
    		logger.info("返回页面已跳转");
    		return false;
    	}
    	try {
    		Document document = Jsoup.parse(html);
        	Elements trs= document.select("div[class=Common_list-table]").get(0).select("tr");
        	if(trs.text().contains("抱歉，没有搜到您想找的简历")){
        		logger.warn("该搜索条件没有可搜的简历 ,关键词：{},期望工作地区:{}" ,keyword ,expAddress);
        		return false;
        	}
        	//从当前页面提取__VIEWSTATE字段，以便更新下次翻页入参
        	if(param !=null){
        		param.put("__VIEWSTATE", document.getElementById("__VIEWSTATE").val());
        	}
    		for(int i=1;i<trs.size();i+=2){
    			Map<String,Object> dataTemp = new HashMap<>();
    			Elements basicTds = trs.get(i).getElementsByTag("td");
    			String[] tds = basicTds.text().split(" ");
    			String id = tds[0];
    			if(RedisUtil.exist51job(id)){
    				if(duplicate_count < 10){
    					duplicate_count++;
    					continue;
    				}else{
    					logger.warn("关键词：{},期望工作地点：{},重复计数超过10次，中断线程" ,keyword ,expAddress);
    					return false;//重复计数超过10次则中断线程
    				}
    			}
				dataTemp.put("ID", (Object)id);
				dataTemp.put("年龄", (Object)tds[1]);
				dataTemp.put("工作年限", (Object)tds[2]);
				dataTemp.put("性别", (Object)tds[3]);
				dataTemp.put("居住地", (Object)tds[4]);
				dataTemp.put("职能", (Object)tds[5]);
				dataTemp.put("学历", (Object)tds[6]);
				dataTemp.put("简历更新时间", (Object)tds[7]);
				dataTemp.put("简历链接", (Object)("http://ehire.51job.com"+basicTds.get(1).select("a").get(0).attr("href")));
    			dataTemp.put("关键词", keyword);
    			dataTemp.put("年龄范围", ageMin+"-"+ageMax);
    			dataTemp.put("期望工作地区", PropertiesUtil.getNameByCode(expAddress));
    			Element detailTr = trs.get(i+1);
    			String detailStr = detailTr.tagName("script").html();
    			detailStr = detailStr.substring(detailStr.indexOf("(")+1, detailStr.lastIndexOf(")"));
    			String[] detailParam  = detailStr.replace("'", "").split(",");
    			String detailUrl = "http://ehire.51job.com/ajax/Common/GlobalAjaxResumeInfo.aspx";
    			Map<String, String> detailHeader = new HashMap<>();
    			detailHeader.put("Referer", "http://ehire.51job.com/Candidate/SearchResumeNew.aspx");
    			detailHeader.put("Cookie", cookie);
    			Map<String, String> detailParams = new HashMap<>();
    			detailParams.put("doType", "FetchResumeContent");
    			detailParams.put("SeqID", detailParam[3]);
    			detailParams.put("UserID", detailParam[0]);
    			detailParams.put("strKey", detailParam[1]);
    			detailParams.put("strLang", detailParam[6]);
    			String detailHtml = NetUtil.loadPageRawTextByGet(detailUrl, client, detailHeader, detailParams);
    			dataTemp.put("快照", (Object)extractText(detailHtml));
    			MongodbUtil.set51job(dataTemp);
    			RedisUtil.set51job(id);
    			logger.info("单条数据：{}" ,dataTemp.toString());
    		}
    		return true;
		} catch (Exception e) {
			logger.error("单个页面解析发生异常,html:{}" , html ,e);
			return false;
		}
    }
    
    //从请求的第一页获取参数进行打包构造第二页的post入参
    public static Map<String, String> packageTurnpageParam(Document document){
    	Map<String, String> param = new HashMap<>();
    	param.put("__EVENTARGUMENT", "");
		param.put("__LASTFOCUS", "");
		param.put("__VIEWSTATE", document.getElementById("__VIEWSTATE").val());
		param.put("ctrlSerach$search_keyword_txt", document.getElementById("ctrlSerach_search_keyword_txt").val());
		param.put("ctrlSerach$search_company_txt", "");
		param.put("ctrlSerach$search_area_input", "");
		param.put("ctrlSerach$search_area_hid", "");
		param.put("ctrlSerach$search_funtype_hid", "");
		param.put("ctrlSerach$search_expectsalaryf_input", "不限");
		param.put("ctrlSerach$search_expectsalaryt_input", "不限");
		param.put("ctrlSerach$search_industry_hid", "");
		param.put("ctrlSerach$search_wyf_input", "不限");
		param.put("ctrlSerach$search_wyt_input", "不限");
		param.put("ctrlSerach$search_df_input", "不限");
		param.put("ctrlSerach$search_dt_input", "不限");
		param.put("ctrlSerach$search_cursalaryf_input", "不限");
		param.put("ctrlSerach$search_cursalaryt_input", "不限");
		param.put("ctrlSerach$search_age_input", document.getElementById("ctrlSerach_search_age_input").val());
		param.put("ctrlSerach$search_agef_input", document.getElementById("ctrlSerach_search_agef_input").val());
		param.put("ctrlSerach$search_aget_input", document.getElementById("ctrlSerach_search_aget_input").val());
		param.put("ctrlSerach$search_expjobarea_input", document.getElementById("ctrlSerach_search_expjobarea_input").val());
		param.put("ctrlSerach$search_expjobarea_hid", document.getElementById("ctrlSerach_search_expjobarea_hid").val());
		param.put("ctrlSerach$search_forlang_input", "语言");
		param.put("ctrlSerach$search_fl_input", "不限");
		param.put("ctrlSerach$search_fllsabilityll_input", "不限");
		param.put("ctrlSerach$search_englishlevel_input", "英语等级");
		param.put("ctrlSerach$search_sex_input", "性别");
		param.put("ctrlSerach$search_major_input", "专业");
		param.put("ctrlSerach$search_major_hid:", "");
		param.put("ctrlSerach$search_hukou_input", "户口");
		param.put("ctrlSerach$search_hukou_hid:", "");
		param.put("ctrlSerach$search_rsmupdate_input", document.getElementById("ctrlSerach_search_rsmupdate_input").val());
		param.put("ctrlSerach$search_jobstatus_input", "求职状态");
		param.put("send_cycle", "1");
		param.put("send_time", "7");
		param.put("send_sum", "10");
		param.put("ctrlSerach$hidSearchValue", document.getElementById("ctrlSerach_hidSearchValue").val());
		param.put("ctrlSerach$hidKeyWordMind", document.getElementById("ctrlSerach_hidKeyWordMind").val());
		param.put("ctrlSerach$hidRecommend", "");
		param.put("ctrlSerach$hidWorkYearArea", "");
		param.put("ctrlSerach$hidDegreeArea", "");
		param.put("ctrlSerach$hidSalaryArea", "");
		param.put("ctrlSerach$hidCurSalaryArea", "");
		param.put("ctrlSerach$hidIsRecDisplay", document.getElementById("ctrlSerach_hidIsRecDisplay").val());
		param.put("showselected", "");
		param.put("pagerTopNew$ctl06", "50");//暂时定为50条每页
		param.put("cbxColumns$0", document.getElementById("cbxColumns_0").val());
		param.put("cbxColumns$1", document.getElementById("cbxColumns_1").val());
		param.put("cbxColumns$2", document.getElementById("cbxColumns_2").val());
		param.put("cbxColumns$3", document.getElementById("cbxColumns_3").val());
		param.put("cbxColumns$4", document.getElementById("cbxColumns_4").val());
		param.put("cbxColumns$5", document.getElementById("cbxColumns_5").val());
		param.put("cbxColumns$6", document.getElementById("cbxColumns_6").val());
		param.put("hidAccessKey", document.getElementById("hidAccessKey").val());
		param.put("hidShowCode", document.getElementById("hidShowCode").val());
		param.put("hidDisplayType", document.getElementById("hidDisplayType").val());
		param.put("hidEhireDemo", "");
		param.put("hidUserID", "");
		param.put("hidCheckUserIds", document.getElementById("hidCheckUserIds").val());
		param.put("hidCheckKey", document.getElementById("hidCheckKey").val());
		param.put("hidEvents", "");
		param.put("hidNoSearchIDs", "");
		param.put("hidBtnType", "");
		param.put("hideMarkid", "");
		param.put("hidStrAuthority", document.getElementById("hidStrAuthority").val());
		param.put("hidDownloadNum", document.getElementById("hidDownloadNum").val());
		param.put("hidKeywordCookie", document.getElementById("hidKeywordCookie").val());
		param.put("showGuide", "");
		return param;
    }
    
    public static String extractText(String data){
    	if(StringUtil.isNullOrEmpty(data))
    		return null;
    	return data.replace("00<", "<").replaceAll("<.*?>", "")
    			.replaceAll("&nbsp;", "").replaceAll("&#x20;", "")
    			.replace("|", "\n").replaceAll("]]>", "")
    			.replaceAll("<span title=", "");
    }
}
